import { ref, computed } from 'vue'

export default function useWindowsManager() {
  const activeWindow = ref(null)
  const isMaximized = ref(false)
  const windowPos = ref({ x: 100, y: 100 })
  const showStartMenu = ref(false)
  const currentTime = ref('')

  const openApp = (app) => {
    activeWindow.value = { ...app }
  }

  const toggleApp = (app) => {
    if (activeWindow.value && activeWindow.value.name === app.name) {
      activeWindow.value = null
    } else {
      activeWindow.value = app
    }
  }

  const closeWindow = () => {
    activeWindow.value = null
  }

  const minimizeWindow = () => {
    activeWindow.value = null
  }

  const toggleMaximize = () => {
    isMaximized.value = !isMaximized.value
  }

  const toggleStartMenu = () => {
    showStartMenu.value = !showStartMenu.value
  }

  const selectMenuItem = (item) => {
    showStartMenu.value = false
    openApp(item)
  }

  const updateTime = () => {
    const now = new Date()
    currentTime.value = now.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
  }

  const shutdown = () => {
    alert('系统将关机')
  }

  const restart = () => {
    alert('系统将重新启动')
  }

  return {
    activeWindow,
    isMaximized,
    windowPos,
    currentTime,
    showStartMenu,
    openApp,
    toggleApp,
    closeWindow,
    minimizeWindow,
    toggleMaximize,
    toggleStartMenu,
    selectMenuItem,
    updateTime,
    shutdown,
    restart
  }
}